package com.bw.service.impl;

import com.bw.dao.UserDao;
import com.bw.dao.UserRoleDao;
import com.bw.pojo.Role;
import com.bw.pojo.User;
import com.bw.pojo.vo.GetUserDetailsVo;
import com.bw.pojo.vo.UserVo;
import com.bw.service.UserService;
import com.bw.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
    @Override
    public int login(User user, HttpServletRequest req, HttpServletResponse resp) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userMapper = sqlSession.getMapper(UserDao.class);
        //登录 根据 用户名 查询对象信息
        //返回值 代表查询的结果
        //参数  sql 条件
        User u = userMapper.login(user.getUsername());
        //如果u是null 代表 用户名不存在
        //状态1账号错误 2密码错误 3 账号被禁用 4登录成功
        if(null == u){
            sqlSession.close();
            return 1;//账号错误
        }else{
            if(u.getPassword().equals(user.getPassword())){
                //状态
                if(u.getState()==1){
                    sqlSession.close();
                    return 3;
                }else{
                    //session
                    req.getSession().setAttribute("user", u);
                    //cookie
                    //1.创建 cookie 2.设置过期时间 3.
                    Cookie name = new Cookie("name", u.getUsername());
                    Cookie pwd = new Cookie("pwd", u.getPassword());
                    name.setMaxAge(7*24*60*60);
                    pwd.setMaxAge(7*24*60*60);
                    name.setPath("/");
                    pwd.setPath("/");
                    resp.addCookie(name);
                    resp.addCookie(pwd);
                    sqlSession.close();
                    return 4;
                }
            }else{
                sqlSession.close();
                return 2;//密码错误
            }
        }

    }
//查看
    @Override
    public List<User> getUserList(User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userMapper = sqlSession.getMapper(UserDao.class);
        //调用 dao 层 方法
        List<User> list = userMapper.getUserList(user);
        sqlSession.close();
        return list;

    }
//添加
    @Override
    public Integer addUser(User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userMapper = sqlSession.getMapper(UserDao.class);
        User userRes = userMapper.login(user.getUsername());
        int res = 0;
        if(null != userRes){
            res =1;
        }else{
            user.setPassword("123");
            userMapper.addUser(user);
            sqlSession.commit();
            res = 2;
        }
        sqlSession.close();
        return res;
    }
    //删除
    @Override
    public void delUser(String uids) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userMapper = sqlSession.getMapper(UserDao.class);
        userMapper.delUser(uids);
        sqlSession.commit();
        sqlSession.close();
    }
    //回显
    @Override
    public User finById(Integer uid) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userMapper = sqlSession.getMapper(UserDao.class);
        User byId = userMapper.findById(uid);
        sqlSession.close();
        return byId;
    }
    //修改
    @Override
    public void updUser(User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userMapper = sqlSession.getMapper(UserDao.class);
        userMapper.updUser(user);
        sqlSession.commit();
        sqlSession.close();
    }
    //批删
    @Override
    public void delUserAll(String ids) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userMapper = sqlSession.getMapper(UserDao.class);
        String[] split = ids.split(",");
        userMapper.userDel(split);
        sqlSession.commit();
        sqlSession.close();
    }
    //批添加
    @Override
    public void userAddAll(List<User> list) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userMapper = sqlSession.getMapper(UserDao.class);
        userMapper.userAddAll(list);
        sqlSession.commit();
        sqlSession.close();
    }

    //查看用户对应的角色
    @Override
    public UserVo getUserByUserId(Integer uid) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userMapper = sqlSession.getMapper(UserDao.class);
        UserVo userByUserId = userMapper.getUserByUserId(uid);
        sqlSession.close();
        return userByUserId;
    }

    @Override
    public List<UserVo> getUserListByMap(UserVo userVo) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userMapper = sqlSession.getMapper(UserDao.class);
        List<UserVo> userListByMap = userMapper.getUserListByMap(userVo);
        sqlSession.close();
        return userListByMap;
    }

    @Override
    public void getRoleListUser(Integer uid) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userMapper = sqlSession.getMapper(UserDao.class);
        userMapper.getRoleListUser(uid);
        sqlSession.close();
    }

}
